<?php session_start(); ?>
<?php include_once('misc/class_config.php'); ?>
<?php
	include_once('access_control/page_access.php');
	declinePageIfNotLoggedIn();
	declinePageIfNotAdmin();
	$currentUser= new user();
	$currentUser->isLoggedIn();
	$currentUser->isTimedOut();
?>


<?php
	include_once('model/category.php');

	$newCategory = new category();
	$categoryMessage = '';
	
	$categoryNameErrorMessage = '';
	$categoryErrorMessage = '';
	$unitPriceErrorMessage = '';
	$descriptionErrorMessage = '';
	$quantityErrorMessage = '';
	
	if(isset($_POST['btnAddCategory']))
	{
		$categoryValidationError = new validation();

		$categoryNameErrorMessage = $categoryValidationError->isEmpty(trim($_POST['name']),'<label class="required">First name is required.</label>');
		$descriptionErrorMessage = $categoryValidationError->isEmpty(trim($_POST['description']),'<label class="required">Description is required.</label>');

		$newCategory->setCategory('',$_POST['name'],$_POST['description']);		
	}
?>

<?php
	if(isset($_POST['btnAddCategory']))
	{
		$formError = $categoryValidationError->status;
		
		if($formError==0)
		{
			try
			{
				if($newCategory->addCategory())
				{
					$categoryMessage = "Category added successfully.";
				}
				else
				{
					$categoryMessage = 'An error occured. Category was not added.';
				}
			}
			catch(Exception $ex)
			{
				$categoryMessage = $ex->getMessage();
			}
		}
	}
?>

<?php $pageTitle = "Add Category | Everything"; ?>
<?php include_once('layout/page_start.php'); ?>
<?php include_once('layout/page_header.php'); ?>
<?php include_once('layout/menu_bar.php'); ?>

<div id="main_content_div">
	<?php include("layout/configuration_menu_bar.php"); ?>
	<div id="add_category_form_div" class="form_div">
		<h2>Add Category</h2>
		<form action="addcategory.php" method="post">
			<table>
				<tr>
					<td><label>Category Name:</label></td> <td><input type="text" name="name" value="<?php if(isset($_POST['btnAddCategory'])){ echo $newCategory->name;  }?>"/> &nbsp &nbsp <?php echo $categoryNameErrorMessage; ?></td>
				</tr>
				<tr>
					<td><label>Description:</label></td> <td><input type="text" name="description" value="<?php if(isset($_POST['btnAddCategory'])){ echo $newCategory->description; }?>" />  &nbsp &nbsp <?php echo $descriptionErrorMessage; ?></td>
				</tr>
			</table>
			<input type="submit" name="btnAddCategory" value="Add Category"/>
			<input type="reset" name="btnReset" value="Reset Fields"/> <br/><br/>
			<label><?php if(isset($categoryMessage)){echo $categoryMessage;} ?></label>
		</form>
	</div></br>
</div>
<?php include('layout/page_footer.php'); ?>
<?php include('layout/page_end.php'); ?>